package net.towee.client.com.me.presenter;

import java.util.Date;
import java.util.List;
import java.util.Map;

import net.towee.client.com.me.Me;
import net.towee.client.managers.AccountHelper;
import net.towee.model.account.AccountIdentity;
import net.towee.model.account.AccountProfile;
import net.towee.model.account.Connection;
import net.towee.model.account.Gender;
import net.towee.model.account.SocialStatus;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;

public class MePresenter implements Me, ClickHandler {

	public interface Display extends IsWidget {
		/*
		 * Profile getters
		 */
		String getFirstName();
		
		String getLastName();
		
		Date getDob();
		
		Gender getGender();
		
		String getStatus();
		
		SocialStatus getSocialStatus();
		
		String getLittleAbout();
		
		List<Connection> getConnections();
		
		Map<String, String> getExtendedProfile();
		
		/*
		 * Profile setters
		 */
		void setFirstName(String name);
		
		void setLastName(String name);
		
		void setDob(Date dob);
		
		void setGender(String name);
		
		void setConnections(List<Connection> connections);
		
		void setLittleAbout(String about);
		
		void setSocialStatus(SocialStatus status);
		
		void setStatus(String status);
		
		void setExtendedProfile(Map<String, String> extProfile);
		
		/*
		 * Buttons
		 */
		HasClickHandlers getNextStepButton();
		
		HasClickHandlers getCloseButton();
	}
	
	// Injected
	private final AccountHelper accountHelper;
	private final Display display;
	
	@Inject
	public MePresenter(AccountHelper accountHelper, Display display) {
		this.accountHelper = accountHelper;
		this.display = display;
		
		display.getCloseButton().addClickHandler(this);
		display.getNextStepButton().addClickHandler(this);
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add( display.asWidget() );
		
		// Set defined values
		display.setFirstName( accountHelper.getAccount().getAccountIdentity().getFirstName() );
		display.setLastName( accountHelper.getAccount().getAccountIdentity().getLastName() );
	}


	public boolean validateStep() {
		AccountProfile profile = accountHelper.getAccount().getProfile();
		profile.setDob(display.getDob());
		profile.setGender(display.getGender());
		profile.setLittleAbout(display.getLittleAbout());
		profile.setSocialStatus(display.getSocialStatus());
		profile.setStatus(display.getStatus());
		profile.setConnections(display.getConnections());
		
		AccountIdentity id = accountHelper.getAccount().getAccountIdentity();
		id.setFirstName( display.getFirstName() );
		id.setLastName( display.getLastName() );
		
		accountHelper.updateAccountIdentity(new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {}
			@Override
			public void onSuccess(Boolean result) {}
		});
		
		accountHelper.updateAccountProfile(new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {}
			@Override
			public void onSuccess(Boolean result) {}
		});
		
		return true;
	}

	@Override
	public void onClick(ClickEvent event) {
		Object source = event.getSource();
	}
}
